//package com.wayhome.srpingbootmybatis.metrics;
//
//import com.wayhome.srpingbootmybatis.metrics.config.ExportConfig;
//import io.prometheus.client.Collector;
//import io.prometheus.client.exporter.HTTPServer;
//import io.prometheus.client.hotspot.*;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.DisposableBean;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.net.InetSocketAddress;
//import java.util.List;
//
//@Component
//public class MetricsStarter implements InitializingBean, DisposableBean {
//
//    private Logger logger = LoggerFactory.getLogger(MetricsStarter.class);
//
//    private HTTPServer server;
//
//    @Autowired
//    private ExportConfig exportConfig;
//
//    @Autowired
//    private List<Collector> collectorList;
//
//    private volatile boolean init;
//
//    @Override
//    public synchronized void afterPropertiesSet() throws Exception {
//        if (!init) {
//            try {
//                logger.info("Start prometheus HTTPServer on port {}.", exportConfig.getPort());
//                server = new HTTPServer(new InetSocketAddress(exportConfig.getPort()), PrefixFilteredCollectorRegistry.getInstance());
//            } catch (IOException e) {
//                logger.warn("Unable to start prometheus HTTPServer.", e);
//                return;
//            }
//            try {
//                // JVM exports
//                new StandardExports().register(PrefixFilteredCollectorRegistry.getInstance());
//                new MemoryPoolsExports().register(PrefixFilteredCollectorRegistry.getInstance());
//                new BufferPoolsExports().register(PrefixFilteredCollectorRegistry.getInstance());
//                new GarbageCollectorExports().register(PrefixFilteredCollectorRegistry.getInstance());
//                new ThreadExports().register(PrefixFilteredCollectorRegistry.getInstance());
//                new ClassLoadingExports().register(PrefixFilteredCollectorRegistry.getInstance());
//                new VersionInfoExports().register(PrefixFilteredCollectorRegistry.getInstance());
//                for (Collector collector : collectorList) {
//                    collector.register(PrefixFilteredCollectorRegistry.getInstance());
//                }
//            } catch (Throwable t) {
//                logger.warn("Unable to initialize server exports.", t);
//            }
//            init = true;
//        }
//    }
//
//
//    @Override
//    public void destroy() throws Exception {
//        server.stop();
//    }
//}
